//
//  HQHCollectionViewHeader.m
//  LastOne
//
//  Created by 小柠檬 on 15/11/3.
//  Copyright © 2015年 sc. All rights reserved.
//

#import "HQHCollectionViewHeader.h"
#import "HTTPMethod.h"
#import "ChangeString.h"
#import "HQHScrollViewModel.h"
#import <UIImageView+WebCache.h>
#import "FitHeader.h"
@implementation HQHCollectionViewHeader

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self getScrollViewData];
        self.dataSourceArray = [NSMutableArray array];
        self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 375 * FitWidth, 180 * FitHeight)];
        _scrollView.contentSize = CGSizeMake(375 * FitWidth * 5, 0);
        _scrollView.contentOffset = CGPointMake(375 * FitWidth, 0);
        _scrollView.pagingEnabled = YES;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.delegate = self;
        [self.contentView addSubview:_scrollView];
        
        if (_myTimer == nil) {
            self.myTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
        }
       
        
        
    }
    return self;
}

-(void)getScrollViewData
{
    [HTTPMethod getDataByString:@"http://api2.pianke.me/read/columns" BodyDic:[ChangeString changeStringToDictionary:@"auth=&client=1&deviceid=137794B8-2578-4119-B189-3E532E7C0F5B&version=3.0.6"] WithDataBlock:^(id data) {
        NSDictionary *dataDic = [data objectForKey:@"data"];
        _carouselArray = [dataDic objectForKey:@"carousel"];
        for (NSDictionary *carouselDic in _carouselArray) {
            HQHScrollViewModel *scrollViewModel = [[HQHScrollViewModel alloc]init];
            [scrollViewModel setValuesForKeysWithDictionary:carouselDic];
            [_dataSourceArray addObject:scrollViewModel];
        }
        for (NSInteger i = 1; i <= 3; i++) {
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i * 375 * FitWidth, 0, 375 * FitWidth, 180 * FitHeight)];
            HQHScrollViewModel *model = _dataSourceArray[i - 1];
            [imageView sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:[UIImage imageNamed:@"placeholder"]];
            
            imageView.tag = 1000 + i;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
            imageView.userInteractionEnabled = YES;
            [imageView addGestureRecognizer:tap];
            [_scrollView addSubview:imageView];
        }
        UIImageView *frontImage = [[UIImageView alloc]init];
        frontImage.frame = CGRectMake(0, 0, 375 * FitWidth, 180 * FitHeight);
        [frontImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", [[_carouselArray objectAtIndex:2] objectForKey:@"img"]]] placeholderImage:[UIImage imageNamed:@"placeholder"]];
        frontImage.tag = 1000 + 3;
        UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        frontImage.userInteractionEnabled = YES;
        [frontImage addGestureRecognizer:tap2];
        
        
        UIImageView *lastImage = [[UIImageView alloc]init];
        lastImage.frame = CGRectMake(375 * FitWidth * 4, 0, 375 * FitWidth, 180 * FitHeight);
        [lastImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", [[_carouselArray objectAtIndex:0] objectForKey:@"img"]]] placeholderImage:[UIImage imageNamed:@"placeholder"]];
        lastImage.tag = 1000 + 1;
        UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        lastImage.userInteractionEnabled = YES;
        [lastImage addGestureRecognizer:tap3];
        
        [_scrollView addSubview:frontImage];
        [_scrollView addSubview:lastImage];
        
    }];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (_scrollView.contentOffset.x > 375 * FitWidth * 4 - 1) {
        _scrollView.contentOffset = CGPointMake(375 * FitWidth, 0);
    }
    else if (_scrollView.contentOffset.x < 1)
    {
        _scrollView.contentOffset = CGPointMake(375 * FitWidth * 3, 0);
    }
}

-(void)tapAction:(UITapGestureRecognizer *)tap
{
    UIImageView *tapImageView = (UIImageView *)tap.view;
    NSInteger i = tapImageView.tag - 1001;
    
    NSString *str = [_carouselArray[i]objectForKey:@"url"];
    NSArray *array = [str componentsSeparatedByString:@"//"];
    NSArray *arr = [array[1]componentsSeparatedByString:@"/"];
    [self.myDelegate ScrollViewPush:arr[1]];
}

-(void)changeImage
{
    if (_scrollView.contentOffset.x == 375 * FitWidth * 4) {
        [_scrollView setContentOffset:CGPointMake(375 * FitWidth, 0) animated:YES];
    }
    else
    {
        [_scrollView setContentOffset:CGPointMake(_scrollView.contentOffset.x + 375 * FitWidth, 0) animated:YES];
    }
}







/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
